package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

/**
 * Created by 80002023 on 2016/7/26.
 */
public class SpriteDemo extends ApplicationAdapter {

    private Pixmap mPixmap;
    private Texture mTexture;
    private TextureRegion mTextureRegion;
    private Sprite mSprite;
    private SpriteBatch mSpriteBatch;
    private OrthographicCamera mCamera;

    private float timer;

    @Override
    public void create() {

        mPixmap = new Pixmap(Gdx.files.internal("gremlins.png"));
        mTexture = new Texture(mPixmap);
        mTextureRegion = new TextureRegion(mTexture, 0, 0, 30, 30);
        mSprite = new Sprite(mTextureRegion);
        mSpriteBatch = new SpriteBatch();
        // 镜头需要设定宽高和镜头中间位置
        mCamera = new OrthographicCamera(800, 480);
        mCamera.position.set(800 / 2, 480 / 2, 0);

        mSprite.setPosition(800 / 2, 480 / 2);
    }

    @Override
    public void render() {
        // 镜头的更新与设置矩阵到SpriteBatch
        mCamera.update();
        mSpriteBatch.setProjectionMatrix(mCamera.combined);

        timer += Gdx.graphics.getDeltaTime();
        // 1.移动
        if (timer < 3) {
            //每秒移动50像素
            mSprite.translateX(50 * Gdx.graphics.getDeltaTime());
        }
        // 2.放缩
        else if (timer > 3 && timer < 6) {
            //每秒xy方向上放大1倍
            mSprite.setPosition(800 / 2, 480 / 2);
            mSprite.scale(1 * Gdx.graphics.getDeltaTime());
        }
        // 3.翻转
        else if (timer > 6 && timer < 9) {
            //每秒旋转90度
            mSprite.setScale(1, 1);
            mSprite.rotate(90 * Gdx.graphics.getDeltaTime());
        } else {
            timer = 0;
            mSprite.setPosition(800 / 2, 480 / 2);
            mSprite.setScale(1, 1);
            mSprite.setRotation(0);
        }
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        mSpriteBatch.begin();
        mSprite.draw(mSpriteBatch);
        mSpriteBatch.end();
    }

    @Override
    public void dispose() {

        // 所有实现Disposable接口的都需要释放资源
        mPixmap.dispose();
        mTexture.dispose();
        mSpriteBatch.dispose();

    }
}
